package com.itbaizhan.openhis.controller.system;

import com.itbaizhan.openhis.domain.User;
import com.itbaizhan.openhis.dto.UserDto;
import com.itbaizhan.openhis.service.UserService;
import com.itbaizhan.openhis.utils.ShiroSecurityUtils;
import com.itbaizhan.openhis.vo.AjaxResult;
import com.itbaizhan.openhis.vo.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * @Description:用户管理控制层
 */
@RestController
@RequestMapping("system/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 分页查询
     * @param userDto
     * @return
     */
    @GetMapping("listUserForPage")
    public AjaxResult listUserForPage(UserDto userDto){
        DataGridView dataGridView = userService.listUserForPage(userDto);
        return AjaxResult.success("查询成功",dataGridView.getData(),dataGridView.getTotal());
    }

    /**
     * select all user
     * @return
     */
    @GetMapping("selectAllUser")
    public AjaxResult selectAllUser() {
        List<User> list = userService.selectAllUser();
        return AjaxResult.success(list);
    }
    /**
     * 添加用户
     * @param userDto
     * @return
     */
    @PostMapping("addUser")
    public AjaxResult addUser(@Validated UserDto userDto){
        userDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(userService.addUser(userDto));
    }

    /**
     * 修改用户
     * @param userDto
     * @return
     */
    @PutMapping("updateUser")
    public AjaxResult updateUser(@Validated UserDto userDto){
        userDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(userService.updateUser(userDto));
    }

    /**
     * 删除用户
     * @param userIds
     * @return
     */
    @DeleteMapping("deleteUserByIds/{userIds}")
    public AjaxResult deleteUserByIds(@Validated @PathVariable
                                      @NotEmpty(message = "需要删除的ID不能为空") Long[] userIds){
        return AjaxResult.toAjax(userService.deleteUserByIds(userIds));
    }

    /**
     * 重置密码
     * @param userIds
     * @return
     */
    @PostMapping("resetPwd/{userIds}")
    public AjaxResult resetPwd(@NotEmpty(message = "需要重置密码的ID不能为空")
                                   @PathVariable Long[] userIds){
        if (userIds.length > 0){
            userService.resetPwd(userIds);
            return AjaxResult.success("重置成功");
        }
        return AjaxResult.success("重置失败,请重试");
    }
}
